using Config;
using MoonFramework.Scripts.Event;
using MoonFramework.Scripts.Tool.System.Setting;
using MoonFramework.Scripts.View.UI;
using MoonFramework.Template;
using MoonFramework.Test;
using Tool;
using Tool.Name;
using UnityEngine;
using UnityEngine.UI;

namespace UI
{
    [UIElement(false, "UI/UI_MainInfoWindow", 0)]
    public class UI_MainInfoWindow : BaseUIWindow
    {
        [SerializeField] private Image timeStateImg;
        [SerializeField] private Text dayText;
        [SerializeField] private Sprite[] timeStateSprites;
        [SerializeField] private Image hPImg;
        [SerializeField] private Image hungerImg;
        private static PlayerConfig playerConfig => ConfigManager.Instance.GetConfig<PlayerConfig>(ConfigName.Player, 0);
        
        protected override void RegisterEvent()
        {
            base.RegisterEvent();
            EventCenter.Instance.Register<bool>(EventName.UpdateTimeStat);
            EventCenter.Instance.Register<int>(EventName.UpdateDay);
            EventCenter.Instance.Register<float>(EventName.UpdateHp);
            EventCenter.Instance.Register<float>(EventName.UpdateHunger);
        }

        protected override void SendEvent()
        {
            base.SendEvent();
            EventCenter.Instance.SendCommand<bool>(EventName.UpdateTimeStat, UpdateTimeState);
            EventCenter.Instance.SendCommand<int>(EventName.UpdateDay, UpdateDay);
            EventCenter.Instance.SendCommand<float>(EventName.UpdateHp, UpdateHp);
            EventCenter.Instance.SendCommand<float>(EventName.UpdateHunger, UpdateHunger);
        }

        protected override void CancelEventListener()
        {
            base.CancelEventListener();
            EventCenter.Instance.RemoveCommand<bool>(EventName.UpdateTimeStat, UpdateTimeState);
            EventCenter.Instance.RemoveCommand<int>(EventName.UpdateDay, UpdateDay);
            EventCenter.Instance.RemoveCommand<float>(EventName.UpdateHp, UpdateHp);
            EventCenter.Instance.RemoveCommand<float>(EventName.UpdateHunger, UpdateHunger);
        }

        private void UpdateHp(float hp)
        {
            hPImg.fillAmount = hp / playerConfig.MaxHp;
        }
        
        private void UpdateHunger(float hunger)
        {
            hungerImg.fillAmount = hunger / playerConfig.MaxHunger;
        }
        
        private void UpdateTimeState(bool isSun)
        {
            timeStateImg.sprite = timeStateSprites[isSun ? 0 : 1];
        }

        private void UpdateDay(int day)
        {
            dayText.text = $"Day {day + 1}";
        }
    }
}